﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    LIST_LOSS
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <link href="<%: Url.Content("~/Scripts/flexigrid/css/flexigrid.css") %>" rel="stylesheet"
        type="text/css" />
<div id="dialog-code" title="ชนิดของ Loss">
<table id="tbl" width="98%">
<thead>
<tr>
<td  colspan="2" class="head cell" >ADD / EDIT</td></tr>
</thead>
<tr >
    <td  class="head cell" >CODE</td><td  class=" cell" > <input type="text" class="txt-int" id="txtcode" maxlength="3" /></td>
</tr>
<tr >
    <td  class="head cell" >DESC</td><td  class=" cell" > <input type="text" id="txtdesc" style="width:400px;" /></td>
</tr>
<tr >
    <td  class="head cell" >Active / Calucate</td><td  class=" cell" > <input type="checkbox" id="chkAct" /> 
    Active&nbsp; <input type="checkbox" id="chkCal" /> Calucate</td>
</tr>
<tbody></tbody>
</table>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FeaturedContent" runat="server">
    <div class="ui-tabs ui-widget ui-widget-content ui-corner-all" style="margin: auto;">
        <ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
            <li class="ui-state-default ui-corner-top "><a href="<%:Url.Action("LIST_WASTE")%>">
                WASTE</a> </li>
            <li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active"><a href="<%:Url.Action("LIST_LOSS")%>">LOSS</a> </li>
        </ul>
    </div>
    <div style="margin-left: 10px;">
        <table id="cases" style="display: none;">
        </table>
    </div>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="TableContent" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="ScriptsSection" runat="server">
    <script src="<%: Url.Content("~/Scripts/flexigrid/flexigrid.js")%>" type="text/javascript"></script>
    <script src="<%: Url.Content("~/Scripts/flexigrid/jquery.cookie.js")%>" type="text/javascript"></script>
    
    <script src='<%: Url.Content("~/Scripts/autoNumeric.js") %>' type="text/javascript"></script>
    <script src='<%: Url.Content("~/Scripts/jquery.metadata.js") %>' type="text/javascript"></script>
    <% if (false)
       { %>
    <script src="../../Scripts/jquery-1.7.2.js" type="text/javascript"></script>
    <%} %>
    <script type="text/javascript">
        $(function () {
            function Save_data() {
                var code = "";
                code = $("#txtcode").val();
                desc = $("#txtdesc").val();


                if ((code == "")) {
                    alert("ระบุค่าไม่ครบ"); $("#txtcode").focus();
                    return;
                }

                if (desc == "") {
                    alert("ระบุค่าไม่ครบ"); $("#txtdesc").focus();
                    return;
                }
                var modes = "A";

                if ($("#txtcode").is(':disabled')) {
                    modes = "E";
                }

                var getUsersUrl = '<%=Url.Action("INS_LOSS_DATA") %>';
                var chk = "";
                if ($("#chkAct").is(":checked")) {
                    chk = "X";
                }   
                var cal = "";
                if ($("#chkCal").is(":checked")) {
                    cal = "X";
                }
                callScriptMethod(getUsersUrl, {
                    thedata: {
                        _code: $("#txtcode").val(),
                        _desc: $("#txtdesc").val(),
                        _act: chk,
                        _cal: cal
                    }, Mode: modes

                })


            } // Save


            $('.txt-int').setMask("999").attr("maxlength", 3);
            $("#dialog-code").dialog({
                width: 500,
                height: 300,
                position: {
                    my: 'center',
                    at: 'center'

                },
                autoOpen: false,
                show: "blind",
                buttons: {
                    "Save": function () {
                        Save_data();

                    },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                },
                open: function (event, ui) {
                    if ($("#txtcode").is(':disabled')) {
                        $("#txtdesc").focus();
                    }else
                    $("#txtcode").focus();
                }
            });




            var sURL = '<%=Url.Action("GetLIST_LOSS") %>';
            $("#cases").flexigrid({
                url: sURL,
                dataType: 'json',
                colModel: [
        { display: 'CODE', name: '_code', width: 50, align: 'center', sortable: true },
        { display: 'DESC', name: '_desc', width: 250, align: 'left' },
        { display: 'ACTIVE', name: '_act', width: 50, align: 'center' },
        { display: 'CALCULATE', name: '_cal', width: 70, align: 'center' },
        { display: 'MODIFY BY', name: '_ModifyBy', width: 100, align: 'left' },
        { display: 'MODIFY DATETIME', name: '_ModifyDT', align: 'left' }

        ],
                buttons: [
		{ name: 'Add', bclass: 'add', onpress: test },
				{ separator: true },
				{ name: 'Edit', bclass: 'edit', onpress: test },
				{ separator: true }
		],
                searchitems: [ // ปุ่ม search กำหนดให้ค้นหาจากอะไรได้บ้าง ถ้าต้องการให้ค่า default เป็นอะไร ให้ตั้งค่า isdefault: true
				{display: 'CODE', name: '_code', isdefault: true },
				{ display: 'DESC', name: '_desc' }
				],
                sortname: "_code",  // กำหนดการจัดเรียงเริ่มต้น ว่าต้องการให้เรียงตาม field อะไรในฐานข้อมูล 
                sortorder: "_desc",
                usepager: true,
                title: 'Master Waste',
                rp: 20,
                width: 754,
                height: 400
            });
        });

        $("#cases tbody tr").live("click",
            function (e) {
                var obj = (e.target || e.srcElement); if (obj.href || obj.type) return true;
                $("tr.trSelected").removeClass('trSelected');
                $(this).addClass('trSelected');

            });
        function test(com, grid) {
             if (com == 'Add') {  // เหตุการเมื่อกดปุ่ม add
                $("#txtcode").removeAttr("disabled").val("");
                $("#txtdesc").val("");
                $("#chkAct").attr("checked", "checked");
                $("#chkCal").attr("checked", "checked");
                $("#dialog-code").dialog("open");
            } else if (com == 'Edit') { // เหตุการณ์เมื่อกดปุ่ม Edit 
                var items = $('.trSelected', grid);
                if (items.length > 0) {
                    if (items.length > 1) {
                        alert('กรุณาเลือกข้อมูลทีละแถว!!');
                    } else {
                        var id = items.find("td:first").text();
                        var c = $.trim(items.find("td:eq(2)").text())
                        var cal = $.trim(items.find("td:eq(3)").text())
                        $("#txtcode").val(id).attr("disabled", "disabled");
                        $("#txtdesc").val(items.find("td:eq(1)").text());
                        if (c.length > 0) {
                            $("#chkAct").attr("checked", "checked");
                        } else {

                            $("#chkAct").removeAttr("checked");
                        }
                        if (cal.length > 0) {
                            $("#chkCal").attr("checked", "checked");
                        } else {

                            $("#chkCal").removeAttr("checked");
                        }


                        $("#dialog-code").dialog("open");
                    }
                } else {
                    alert('กรุณาเลือกข้อมูลที่ต้องการแก้ไข!!');
                }
            }
        }

        $.fn.flexgridReload = function (url) {
            if (url) {
                this.flexOptions({
                    url: url,
                    newp: 1
                }).flexReload();
            } else {
                this.flexReload();
            }
        };
        function callScriptMethod(url, jsonObject, action, callback, async) {

            callback = callback || function () { };
            async = (async == null || async);

            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: url,
                data: JSON.stringify(jsonObject),
                dataType: "json",
                async: async,
                success: function (data) {
                    alert(data.message);
                    if (data.success != "0") {
                        $("#cases").flexgridReload();
                        $("#dialog-code").dialog("close");
                    }
                },
                error: function (xhr, status, error) {
                    var err = xhr.responseText;
                    alert(err.Message);
                }
            });
        }

    </script>
</asp:Content>